﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ProjectEulerSolutions
{
    /*
     * The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.

There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

How many circular primes are there below one million?

     * 
     * 
     * 
     * */
    class Problem35
    {
        public static string Calculate()
        {
            //oni koji imaju znamenke 0, 2, 4, 5, 6, 8 nisu cirkularni

            int[] invalidDigits = new int[] { 0, 2, 4, 5, 6, 8 };

            List<int> circularPrimes = new List<int>()
            {
                2, 3, 5, 7
            };

            int limit = 1000000;
            for (int i = 11; i < limit; i += 2) //povećavamo za dva jer svakako parni brojevi nisu.
            {
                if (circularPrimes.Contains(i)) // vec smo da dodali
                    continue;

                var digits = CommonFunctions.GetDigits(i);
                var commonCount = digits.Intersect(invalidDigits).ToList().Count;
                if (commonCount != 0)
                    continue; //sadrzi neke koje netreba

                if (CommonFunctions.IsPrime(i))
                {
                    var rotations = CommonFunctions.RotateNumber(i).Distinct();

                    bool allPrimes = true;
                    foreach (int number in rotations)
                    {
                        if (!CommonFunctions.IsPrime(number))
                        {
                            allPrimes = false;
                            break;
                        }
                    }

                    if (allPrimes)
                    {
                        circularPrimes.AddRange(rotations);
                    }
                }
            }

            foreach (int prime in circularPrimes)
                Console.WriteLine(prime);

            return circularPrimes.Count.ToString();
        }
    }
}
